package com.tidy.seed.admin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tidy.seed.admin.system.dto.LogFindPageDTO;
import com.tidy.seed.admin.system.entity.Log;
import com.tidy.seed.admin.system.service.LogService;
import com.tidy.seed.common.Result;
import com.tidy.seed.util.ObjUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

// ✅ Swagger 引入
import io.swagger.annotations.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HZ-ZY
 * @since 2025-08-06
 */
@RestController
@Api(tags = "Log 接口")
@RequestMapping("/admin/system/log")
public class LogController {

    @Resource
    private LogService logService;

    @ApiOperation(value = "新增或更新Log")
    @PostMapping
    public Result save(@RequestBody @ApiParam("Log 对象") Log log) {
        logService.saveOrUpdate(log);
        return Result.success();
    }

    @ApiOperation(value = "根据 ID 删除Log")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable @ApiParam("ID") Integer id) {
        logService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "批量删除Log")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody @ApiParam("ID 列表") List<Integer> ids) {
        logService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation(value = "查询全部Log")
    @GetMapping
    public Result findAll() {
        return Result.success(logService.list());
    }

    @ApiOperation(value = "根据 ID 查询Log")
    @GetMapping("/{id}")
    public Result findOneById(@PathVariable @ApiParam("ID") Integer id) {
        return Result.success(logService.getById(id));
    }

    @ApiOperation(value = "分页查询Log")
    @GetMapping("/page")
    public Result findPage(@RequestParam @ApiParam("页码") Integer pageNum,
                           @RequestParam @ApiParam("页大小") Integer pageSize,
                           @ModelAttribute @ApiParam("查询参数") LogFindPageDTO dto
    ) {
        LambdaQueryWrapper<Log> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjUtils.isNotNull(dto.getStatus())) queryWrapper.eq(Log::getStatus, dto.getStatus());
        if (ObjUtils.isNotNull(dto.getType())) queryWrapper.eq(Log::getType, dto.getType());
        if (ObjUtils.isNotEmpty(dto.getName())) queryWrapper.like(Log::getName, dto.getName());
        if (ObjUtils.isNotEmpty(dto.getIp())) queryWrapper.like(Log::getIp, dto.getIp());
        queryWrapper.orderByDesc(Log::getId);
        return Result.success(logService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}
